package com.datang.petstore.controller;


import com.datang.petstore.aop.ForbidAnnotation;
import com.datang.petstore.entity.MicroblogComment;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.model.*;
import com.datang.petstore.resultpage.Result;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-07 15:08
 * @notify 微博评论api
 * @version 1.0
 */
@RestController
public class MicroblogCommentController {
    @Autowired
    private MicroblogCommentService microblogCommentService;

    /*
     * @author 顶风少年
     * @date 2019/11/7 15:11
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogCommentRequest
     * @result com.datang.petstore.resultpage.Result
     * @notify 添加评论
     * version 1.0
     */
    @PostMapping(value = "microblog/comment")
    @ForbidAnnotation
    public Result addMicroblogComment(@RequestBody MicroblogCommentRequest microblogCommentRequest) {
        MicroblogComment microblogComment = new MicroblogComment();
        BeanUtils.copyProperties(microblogCommentRequest, microblogComment);
        microblogCommentService.addMicroblogComment(microblogComment);
        return Result.ok();
    }


    /*
     * @author 顶风少年
     * @date 2019/11/7 15:11
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogCommentRequest
     * @result com.datang.petstore.resultpage.Result
     * @notify 添加评论
     * version 1.0
     */
    @GetMapping(value = "microblog/comment/hot")
    @ForbidAnnotation
    public Result getHotMicroblogCommentList(@RequestBody GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        List<GetMicroblogCommentListResponse> hotMicroblogCommentList = microblogCommentService.getHotMicroblogCommentList(getMicroblogCommentListRequest);
        return Result.ok(hotMicroblogCommentList);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/9 20:15
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param getMicroblogCommentListRequest
     * @result java.util.List<com.datang.petstore.model.GetMicroblogCommentListResponse>
     * @notify 获取微博评论
     * version 1.0
     */
    @GetMapping(value = "microblog/comment/list")
    @ForbidAnnotation
    public Result getMicroblogCommentList(@RequestBody GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        List<GetMicroblogCommentListResponse> hotMicroblogCommentList = microblogCommentService.getMicroblogCommentList(getMicroblogCommentListRequest);
        return Result.ok(hotMicroblogCommentList);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/9 20:15
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param getMicroblogCommentListRequest
     * @result java.util.List<com.datang.petstore.model.GetMicroblogCommentListResponse>
     * @notify 获取微博评论
     * version 1.0
     */
    @GetMapping(value = "microblog/comment/list/two")
    @ForbidAnnotation
    public Result getTwoMicroblogCommentList(@RequestBody GetMicroblogCommentListRequest getMicroblogCommentListRequest) {
        List<GetMicroblogTwoCommentListResponse> twoMicroblogCommentList = microblogCommentService.getTwoMicroblogCommentList(getMicroblogCommentListRequest);
        return Result.ok(twoMicroblogCommentList);
    }

    /*
     * @author 顶风少年
     * @date 2019/11/10 14:49
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param deleteMicroblogCommentRequest
     * @result com.datang.petstore.resultpage.Result
     * @notify 逻辑删除微博评论
     * version 1.0
     */
    @DeleteMapping(value = "microblog/comment")
    @ForbidAnnotation
    public Result updateMicroblogComment(@RequestBody DeleteMicroblogCommentRequest deleteMicroblogCommentRequest) {
        MicroblogComment microblogComment = microblogCommentService.getMicroblogComment(deleteMicroblogCommentRequest.getCommentId());
        if(microblogComment.getUserId().equals(deleteMicroblogCommentRequest.getUserId())){
            microblogComment.setDel("1");
            microblogCommentService.updateMicroblogComment(microblogComment);
        }else{
            throw new AcceptResultCodeException(ResultCode.error514);
        }
        return Result.ok();
    }
}
